package com.cwt_taxi_xa;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.db.bll.LocalInfoBiz;
import com.cwt_taxi_xa.model.LocalInfo;
import com.cwt_taxi_xa.protocol.BusinessFactory;
import com.cwt_taxi_xa.protocol.Encoder;
import com.cwt_taxi_xa.protocol.MeterBusinessProcess;
import com.cwt_taxi_xa.record.RecorderBiz;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.GPIOUtil;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.TTsFunction;
import com.cwt_taxi_xa.util.Tools;

import java.util.ArrayList;

public class ActivityPJ extends CWTBaseActivity{

	private String TAG = "ActivityPJ";
	ImageView ic_cancel;
	ImageView erwei1,erwei2;
	TextView[] tvs;
	//金额-里程-上车时间-下车时间-总时长-车牌号
	int[] tvids = {R.id.textView1, R.id.textView5, R.id.textView11, R.id.textView12, R.id.textView13, R.id.textView14};

	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 1001://非常满意
					valuation = 1;
                    AppLog.d(TAG,"评价成功");
                    TTsFunction.speakText("评价成功");
					finish();
					break;
				case 1002://满意
					valuation = 2;
					TTsFunction.speakText("评价成功");
					finish();
					break;
				case 1003://不满意
					valuation = 3;
					TTsFunction.speakText("评价成功");
					finish();
					break;
				case 1004://平价超时
					valuation = 0;
					TTsFunction.speakText("评价超时");
					finish();
					break;
			}


			if(msg.what==1005){
				if(waitPJ){
                    AppLog.d("请对本次服务进行评价");
					TTsFunction.speakText("请对本次服务进行评价");
				}
			}

		};
	};

	boolean waitPJ = false;
	private int valuation = 0;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		try {

			/**
			 * 2020-4-16有个别车出现一条营运记录三次的现象
			 * 添加防范策略
			 * 每次添加记录时，保存上车时间作为一条记录的标识码
			 * 只有未保存过标识码的记录才处理
			 */
			if(MeterBusinessProcess.historyBoardingTime==null){
				MeterBusinessProcess.historyBoardingTime = new ArrayList<>();
			}
			String sTime = Tools.bcd2Str(Tools.byteTobyte(BusinessFactory.meterSingleBusinessData, 15 + 6 + 16 + 19, 5));
			AppLog.e("测试多单营运-上车时间："+sTime);
			boolean theSaved = false;
			for (int i = 0; i < MeterBusinessProcess.historyBoardingTime.size(); i++) {
				if(MeterBusinessProcess.historyBoardingTime.get(i).equals(sTime)){
					theSaved = true;
				}
			}
			AppLog.e("测试多单营运-已保存："+theSaved);
			if(!theSaved){
				MeterBusinessProcess.historyBoardingTime.add(sTime);
				//0：正常营运数据；
				//非0标识电召营运数据；
				//999 终端营运数据；
				byte[]  business = Encoder.getBusinessDataUplpad(valuation , 0);//
				LocalInfo info = new LocalInfo(LocalInfo.TYPE_BUSINESS);
				info.setData(Tools.parseByte2HexStr(business));
				long status1 = LocalInfoBiz.getInstance().addInfo(info);
				AppLog.e(TAG,"运营数据不准测试---保存标识："+status1);
				AppLog.savelog("运营数据不准测试---保存标识："+status1);
				LocalInfoBiz.getInstance().postBusinessInfo();
				if(RecorderBiz.getRecordType() == 2){
					SerialPortService.handler.obtainMessage(1007,valuation).sendToTarget();
				}
			}


		} catch (Exception ex) {
			AppLog.savelog("重转空报错\n"+"---------------------------------------"+
					AppLog.generateTag()+
					"\n"+ex.getLocalizedMessage()+
					"\n"+ex.getMessage()+
					"\n"+CrashHandler.exception(ex)+
					"---------------------------------------");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		AppLog.d(TAG,"onCreate");
		setContentView(R.layout.activity_aa_pj);

		ic_cancel = (ImageView) findViewById(R.id.imageView1);
		ic_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		tvs = new TextView[tvids.length];
		for (int i = 0; i < tvids.length; i++) {
			tvs[i] = (TextView) findViewById(tvids[i]);
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String result = getIntent().getStringExtra("result");
		//AppLog.d(TAG,"onCreate");
		String[] ss = result.split("-");
		if(ss.length==6){
			for (int i = 0; i < ss.length; i++) {
				tvs[i].setText(ss[i]);
			}
			tvs[4].setText(getIntent().getStringExtra("yytime"));
		}
		//应该是计价器播音和评价音重叠了
		handler.sendEmptyMessageDelayed(1005, 5000);
		//AppLog.d(TAG,"onCreate 1005");
		initErwei();

		waitPJ = true;
		new Thread(new Runnable() {
			@Override
			public void run() {
				int i3, i5, i6;
				boolean pingjiaqiback = SPUtils.getParamBoolean("pingjiaqiback", true);
				for (int i = 0; i < 1000 && waitPJ; i++) {
					if(pingjiaqiback){
						i3 = GPIOUtil.readIOState("/proc/input3");
						i5 = GPIOUtil.readIOState("/proc/input5");
						i6 = GPIOUtil.readIOState("/proc/input6");
						if(!(i3==1&&i5==1&&i6==1)){
							if(i3==1){
								handler.sendEmptyMessage(1001);
								waitPJ = false;
								break;
							}else if(i5==1){
								handler.sendEmptyMessage(1002);
								waitPJ = false;
								break;
							}else if(i6==1){
								handler.sendEmptyMessage(1003);
								waitPJ = false;
								break;
							}
						}
					}
					try {
						Thread.sleep(30);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if(waitPJ){
					handler.sendEmptyMessage(1004);
					waitPJ = false;
				}
			}
		}).start();
	}


	private void initErwei() {

		Bitmap bitmap1 = null;
		Bitmap bitmap2 = null;
		if(Constants.sijiinfo!=null){
			try {
				bitmap1 = ActivityCamera.getLoacalBitmap(Constants.sijiinfo.getQrCode1());
			} catch (Exception e) {
			}
			try {
				bitmap2 = ActivityCamera.getLoacalBitmap(Constants.sijiinfo.getQrCode2());
			} catch (Exception e) {
			}
		}
		ImageView e1 = (ImageView) findViewById(R.id.imgerwei1);
		if(bitmap1!=null){
			e1.setImageBitmap(bitmap1);
			e1.setVisibility(View.VISIBLE);
		}else{
			e1.setVisibility(View.GONE);
		}
		ImageView e2 = (ImageView) findViewById(R.id.imgerwei2);
		if(bitmap2!=null){
			e2.setImageBitmap(bitmap2);
			e2.setVisibility(View.VISIBLE);
		}else{
			e2.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		waitPJ = false;
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		switch (keyCode) {
			case 131://满意
				handler.sendEmptyMessage(1001);
				waitPJ = false;
				break;
			case 133://不满意
				handler.sendEmptyMessage(1003);
				waitPJ = false;
				break;
			case 132://一般
				handler.sendEmptyMessage(1002);
				waitPJ = false;
		}

		//		TTsFunction.speakText("评价成功");
		//		finish();
		return true;
	}

}
